﻿using System;
using System.Windows;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace Songhay.Silverlight.BiggestBox.IndexPart.ViewModels
{
    using BiggestBoxServiceReference;
    using Extensions;
    using Songhay.Mvvm.Models;
    using Songhay.Mvvm.ViewModels;
    using Songhay.Mvvm.Views;

    /// <summary>
    /// PI Number View Model
    /// </summary>
    public class PiNumberViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PiNumberViewModel"/> class.
        /// </summary>
        /// <remarks>
        /// For details, see
        /// “How to: Implement Property Change Notification”
        /// http://msdn.microsoft.com/en-us/library/ms743695.aspx
        /// A sample <c>set</c> block:
        /// <code>
        /// set
        /// {
        /// _myProperty = value;
        /// OnPropertyChanged("MyProperty");
        /// }
        /// </code>
        /// </remarks>
        public PiNumberViewModel()
        {
            #region design-time data:

            this.Pi = string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}", "3.1415926535",
                "8979323846",
                "2643383279",
                "5028841971",
                "6939937510",
                "5820974944",
                "5923078164",
                "0628620899",
                "8628034825",
                "3421170679");

            #endregion

            if(this.IsInDesignMode) return;

            #region commanding:

            base.CommandDictionary.Add("service-calc-pi",
            () =>
            {
                this.Pi = string.Empty;
                try
                {
                    var binding = Application.Current.GetServiceClientBinding();
                    var endpointAddress = Application.Current.GetServiceClientEndpointAddress("../../Services/BiggestBoxService.svc");
                    this._client = new BiggestBoxServiceClient(binding, endpointAddress);
                    this._client.CalculatePiCompleted += (s, args) =>
                    {
                        this.Pi = args.Result;
                        this.IsMakingServiceCall = false;
                    };
                    this.Pi = "calculating…";
                    this._client.CalculatePiAsync(10);
                    this.IsMakingServiceCall = true;
                }
                catch(Exception ex)
                {
                    base.IsServiceError = true;
                    base.ServiceErrorMessage = ex.Message;
                }
            });

            #endregion

            #region messaging:

            Messenger.Default.Register<LightMessage<AlertChildWindow>>(this,
                message =>
                {
                    if(message.Source != LightMessageSource.ViewAddedChildren) return;

                    this.Pi = string.Empty;
                });

            Messenger.Default.Register<LightMessage<AlertChildWindow>>(this,
                message =>
                {
                    if(message.Source != LightMessageSource.ViewEventClosing) return;
                    if(this._client == null) return;

                    this._client.CloseConnectionOrAbort();
                });

            #endregion
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is making service call.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is making service call; otherwise, <c>false</c>.
        /// </value>
        public bool IsMakingServiceCall
        {
            get { return this._isMakingServiceCall; }
            set
            {
                this._isMakingServiceCall = value;
                base.RaisePropertyChanged("IsMakingServiceCall");
            }
        }

        /// <summary>
        /// Gets or sets the PI.
        /// </summary>
        /// <value>The PI.</value>
        public string Pi
        {
            get { return this._pi; }
            set
            {
                this._pi = value;
                base.RaisePropertyChanged("Pi");
            }
        }

        /// <summary>
        /// Gets or sets the view unloaded command.
        /// </summary>
        /// <value>The view unloaded command.</value>
        public RelayCommand ViewUnloadedCommand { get; private set; }

        BiggestBoxServiceClient _client;
        string _pi;
        bool _isMakingServiceCall;
    }
}
